package com.efast.dmom.mgmt.service.mes.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.order.WorkOrderItem;
import com.efast.dmom.mgmt.dao.mes.TwBasWorkOrderItemDao;
import com.efast.dmom.mgmt.service.mes.TwBasWorkOrderItemService;
import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;

@Service
public class TwBasWorkOrderItemServiceImpl implements TwBasWorkOrderItemService {

	@Autowired
	private TwBasWorkOrderItemDao twBasWorkOrderItemDao;

	@Override
	public ResultBean workOrderItemSort(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean(true,"'");
		String sortButton = (String)map.get("sortButton");
		if (sortButton!=""&&sortButton.equals("up")) {
			@SuppressWarnings("unchecked")
			List<Map<String,Object>> list = (List<Map<String,Object>>)map.get("params");
			if (list.size()!=1) {
				resultBean.setSuccess(false);
				resultBean.setMessage("请只选择一条数据！");
			} else {
				Map<java.lang.String, Object> map2 = list.get(0);
				String planStartTime = (String)map2.get("planStartTime");
				//通过计划上线时间获取上线序号
				List<String> sequence = twBasWorkOrderItemDao.selectSortNumByPlanStartTime(planStartTime);
				Map<Integer,String> seq =new HashMap<Integer,String>();
				List<Double> num = new ArrayList<Double>();
				int i=0;
				for(String number: sequence) {
					num.add(Double.parseDouble(number));
					seq.put(i++, number);
				}
				//获得移动数据的上线序号
				String sortNum = (String)map2.get("sequence");
				Double sortNum1=Double.parseDouble(sortNum);
				Integer indexOf = num.indexOf(sortNum1);
				if (indexOf==0) {
					resultBean.setSuccess(false);
					resultBean.setMessage("此数据已是本日期第一条数据，如需上移,请手动修改时间后再试！");
				}else {
					String Sequence = seq.get(indexOf-1);
					Integer selectIdBySequence = twBasWorkOrderItemDao.selectIdBySortNum(Sequence);
					Map<String,Object> sortInfo=new HashMap<String,Object>();
					sortInfo.put("currentChangeBeforeId", (Integer)map2.get("id"));
					sortInfo.put("currentChangeAfterId", Sequence);
					sortInfo.put("sortChangeBeforeId", selectIdBySequence);
					sortInfo.put("sortChangeAfterId", sortNum);
					twBasWorkOrderItemDao.sortUpdateId(sortInfo);
					resultBean.setSuccess(true);
					resultBean.setMessage("数据上移成功");
				}
			}
			
		} else if (sortButton!=""&&sortButton.equals("down")) {
			@SuppressWarnings("unchecked")
			List<Map<String,Object>> list = (List<Map<String,Object>>)map.get("params");
			if (list.size()!=1) {
				resultBean.setSuccess(false);
				resultBean.setMessage("请只选择一条数据！");
			} else {
				Map<java.lang.String, Object> map2 = list.get(0);
				String planStartTime = (String)map2.get("planStartTime");
				//通过计划上线时间获取上线序号
				List<String> sequence = twBasWorkOrderItemDao.selectSortNumByPlanStartTime(planStartTime);
				Map<Integer,String> seq =new HashMap<Integer,String>();
				List<Double> num = new ArrayList<Double>();
				int i=0;
				for(String number: sequence) {
					num.add(Double.parseDouble(number));
					seq.put(i++, number);
				}
				//获得移动数据的上线序号
				String sortNum = (String)map2.get("sequence");
				Double sortNum1=Double.parseDouble(sortNum);
				Integer indexOf = num.indexOf(sortNum1);
				if (indexOf==sequence.size()-1) {
					resultBean.setSuccess(false);
					resultBean.setMessage("此数据已是本日期最后一条数据，如需下移,请手动修改时间后再试！");
				}else {
					String Sequence = seq.get(indexOf+1);
					Integer selectIdBySequence = twBasWorkOrderItemDao.selectIdBySortNum(Sequence);
					Map<String,Object> sortInfo=new HashMap<String,Object>();
					sortInfo.put("currentChangeBeforeId", (Integer)map2.get("id"));
					sortInfo.put("currentChangeAfterId", Sequence);
					sortInfo.put("sortChangeBeforeId", selectIdBySequence);
					sortInfo.put("sortChangeAfterId", sortNum);
					twBasWorkOrderItemDao.sortUpdateId(sortInfo);
					resultBean.setSuccess(true);
					resultBean.setMessage("数据下移成功");
				}
			}
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据移动失败");
		}
		return resultBean;
	}

	@Override
	public ResultBean workOrderItemEdit(WorkOrderItem workOrderItem) {
		ResultBean resultBean = new ResultBean(true,"'");
		Integer id = Integer.valueOf(workOrderItem.getId());
		String PlanStartTime = twBasWorkOrderItemDao.selectPlanStartTime(id);
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateTime=dateFormat.format(workOrderItem.getPlanStartTime());
		String endTime=dateFormat.format(workOrderItem.getPlanEndTime());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = null;
		Date date2 = null;
		Date endDate = null;
		try {
			date1 = format.parse(PlanStartTime);
			date2 = format.parse(dateTime);
			endDate = format.parse(endTime);
			if(endDate.getTime() - date2.getTime() >= 0) {
			if(date1.compareTo(date2) == 0) {
		     workOrderItem.setLastUpdateUser(SpringWebUtils.getRequestUser());
		     workOrderItem.setLastUpdateTime(new Date());
		     workOrderItem.setPlanStartTime(date2);
			 workOrderItem.setPlanEndTime(format.parse(endTime));
			 twBasWorkOrderItemDao.updateWorkOrderItem(workOrderItem);
			 resultBean.setSuccess(true);
			  resultBean.setMessage("数据更新成功");
			} else {
			  List<String> selectSortNumByPlanStartTime=twBasWorkOrderItemDao.selectSortNumByPlanStartTime(dateTime);
			  if(selectSortNumByPlanStartTime !=null && selectSortNumByPlanStartTime.size() >0) {
			  String sequence=selectSortNumByPlanStartTime.get(selectSortNumByPlanStartTime.size()-1);
			  Long sortNum1=Long.parseLong(sequence);
			  String sequence1=String.valueOf(sortNum1+1);
			  workOrderItem.setSequence(sequence1);
			  } else {
				 StringBuffer stringBuffer = new StringBuffer("");
			        for (int i = 0; i < dateTime.length(); i++) {
			            if (dateTime.charAt(i) != '-') {
			                stringBuffer.append(dateTime.charAt(i));
			            }
			      }
			    String currentDate = dateFormat.format(new Date()).substring(8);
				workOrderItem.setSequence(stringBuffer.toString().concat(currentDate).concat("0001")); 
			  }
			  workOrderItem.setLastUpdateUser(SpringWebUtils.getRequestUser());
			  workOrderItem.setLastUpdateTime(new Date());
			  workOrderItem.setPlanStartTime(date2);
			  workOrderItem.setPlanEndTime(format.parse(endTime));
			  twBasWorkOrderItemDao.updateWorkOrderItem(workOrderItem);
			  resultBean.setSuccess(true);
			  resultBean.setMessage("含计划开始时间的数据修改成功！");
			}
			} else {
			  resultBean.setSuccess(false);
		      resultBean.setMessage("计划开始时间不得大于计划完成时间！");
		      return resultBean;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resultBean;
	}
}
